package day10.test1_01;

class a{
    private int a;

    public a(int a) {
        this.a = a;
    }

    public int getA() {
        return a;
    }

    public synchronized void chang(int a){
        System.out.println(Thread.currentThread().getName()+"正在修改value值："+this.a);
        this.a+=a;
        try{
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(Thread.currentThread().getName()+"正在修改value值："+this.a);
    }
}

class TestThread extends Thread{
    private a a;
    private int value;

    public TestThread(day10.test1_01.a a, int value) {
        this.a = a;
        this.value = value;
    }

    @Override
    public void run(){
        a.chang(this.value);
    }
}

public class test {
    public static void main(String[] args) throws InterruptedException {
        a a=new a(0);
        TestThread thread1=new TestThread(a,10);
        TestThread thread2=new TestThread(a,-100);
        thread1.start();
        Thread.sleep(1000);
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println(a.getA());
    }
}
